#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <errno.h>
#include <linux/input.h>
#include "mplayer.h"
#include <dirent.h>

#define DISP_BUF_SIZE (600 * 1024)

lv_obj_t* img;
lv_obj_t* img2;
lv_obj_t* img3;
lv_obj_t* ta1;
lv_obj_t* list;
lv_obj_t* bt;
lv_obj_t* lb;
int mark=0;
struct music *head;
lv_obj_t* ta2;
lv_obj_t* bt1;
lv_obj_t* bt2;
lv_obj_t *msgbox=NULL;
const char *btns[]={"continue","close",""};
lv_obj_t *page1;
lv_obj_t* bt3;
lv_obj_t* bt4;
lv_obj_t* bt5;
lv_obj_t* lb4;
lv_obj_t  *kb;
lv_obj_t  *kb2;
lv_obj_t* ta3;
lv_obj_t *page1;
lv_obj_t *page2;
lv_obj_t* ta4;
int mark2=0;

//音乐播放按钮矩阵
const char *map1[]={"start","stop","continue","\n",
                   "prev","next","over",""};
lv_obj_t* lb3;

//我们自己封装的显示中文的函数
int show_chinese(int size,lv_obj_t *obj)
{
	static lv_ft_info_t info;
    info.name = "/font/simfang.ttf";
    info.weight = size;  //字体大小
    info.style = FT_FONT_STYLE_NORMAL;
    info.mem = NULL;
    if(!lv_ft_font_init(&info)) {
        LV_LOG_ERROR("create failed.");
    }

    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_font(&style, info.font);
    lv_style_set_text_align(&style, LV_TEXT_ALIGN_CENTER);

	lv_obj_add_style(obj, &style, 0);
	return 0;
}

//搜索事件响应函数
void search(lv_event_t* e)
{
	mark=1;
	lv_obj_clean(list);
	const char* txt3 = lv_textarea_get_text(ta4);
	head=music_init();
	dirread("/mymusic",txt3);
	if(strcmp(txt3,".mp3")==0)
		music_show(head);
}

void kb_cb(lv_event_t* e)
{
	uint16_t id;
	id = lv_btnmatrix_get_selected_btn(e->target);
	const char* txt = lv_btnmatrix_get_btn_text(e->target,id);
	//printf("你点击了%s\n",txt);
	if(strcmp(txt,"")==0||strcmp(txt,"")==0)
		kb_disappear();
}

//按钮矩阵事件响应函数
void btnmat_cb(lv_event_t* e)
{
	uint8_t id;
	
	id = lv_btnmatrix_get_selected_btn(e->target);
	char *content=lv_btnmatrix_get_btn_text(e->target,id);
	if(strcmp(content,"start")==0)
		music_start(head);
	if(strcmp(content,"stop")==0)
		music_stop();
	if(strcmp(content,"continue")==0)
		music_continue();
	if(strcmp(content,"prev")==0)
		music_prev(head);
	if(strcmp(content,"next")==0)
		music_next();
	if(strcmp(content,"over")==0)
		music_over(head);
}

void kb_disappear()
{
	if(mark2==0)
		lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
	if(mark2==1)
		lv_obj_add_flag(kb2, LV_OBJ_FLAG_HIDDEN);
}

void usrname(lv_event_t* e)
{	
	const char* txt1 = lv_textarea_get_text(ta1);
	const char* txt2 = lv_textarea_get_text(ta2);
	const char *err_msg = NULL;
	if(strcmp(txt1,"manbo")==0&&strcmp(txt2,"123456")==0)
	{
		printf("logining seccess!\n");
		//lv_obj_del(page2);
		show_search();
	}
	if(strcmp(txt1,"gec")!=0)
		err_msg="your username is wrong!";
	if(strcmp(txt2,"123456")!=0)
		err_msg="your password is wrong!";
	if(err_msg!=NULL)
	{
		msgbox = lv_msgbox_create(page2, "error", err_msg, btns, true );
		lv_obj_set_pos(msgbox,300,200);
	}
}

void kb_appear(lv_event_t* e)
{
	lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
	if(e->target==ta1)
		lv_keyboard_set_textarea(kb, ta1);
	if(e->target==ta2)
		lv_keyboard_set_textarea(kb, ta2);
	if(e->target==ta4)
	{
		lv_obj_clear_flag(kb2, LV_OBJ_FLAG_HIDDEN);
		lv_keyboard_set_textarea(kb2, ta4);
	}
}

//递归读取目录函数
int dirread(char path[1024],char *sear)
{
	if (!list) 
	{
		printf("list is NULL!\n");
		return -1;
	}
	DIR *dir;
	char new_path[1024];
	struct dirent *over;
	dir=opendir(path);
	if(dir==NULL)
	{
		perror("目录打开失败！\n");
		return -1;
	}
	while(1)
	{
		over=readdir(dir);
		if(over==NULL)
			break;
		if (strcmp(over->d_name,".")==0||strcmp(over->d_name,"..")==0)
            continue;
		snprintf(new_path, sizeof(new_path),"%s/%s",path,over->d_name);
		if(over->d_type==4)
			dirread(new_path,sear);
		if(over->d_type!=4)
		{
			if(sear==NULL||strstr(new_path, sear)!=NULL)
			{
				lv_list_add_text(list, new_path);
				if(sear!=NULL&&strcmp(sear,".mp3")==0)
				{
					music_install(head,new_path,new_path);
				}
			}
		}
	}
	closedir(dir);
	return 0;
}

void show_login()
{
  mark2=0;
  //创建一个新页面
  page2=lv_obj_create(NULL);
  lv_obj_set_size(page2,800,480);
  lv_scr_load(page2);
  //登录界面制作
  //背景图片
  img = lv_img_create(page2);
  lv_img_set_src(img, "S:/myphoto/background.bmp");
  //lv_obj_align(img, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
  //标题
  lv_obj_t* allb=lv_label_create(page2);
  lv_obj_set_pos(allb,200,80);
  show_chinese(20,allb);
  lv_label_set_text(allb, "欢迎使用曼波登录系统");
  //吉祥物图片
  lv_obj_t* img2=lv_img_create(page2);
  lv_img_set_src(img2, "S:/myphoto/manbo2.bmp");
  //lv_img_set_zoom(img, 32);
  //lv_img_set_zoom(img2, 200);
  //lv_obj_set_pos(img,-750,-500);
  lv_obj_set_pos(img2,130,150);
  //用户名文本框
  ta1 = lv_textarea_create(page2);
  lv_obj_set_pos(ta1,450,150);
  lv_obj_set_size(ta1,200,50);
  lv_textarea_set_placeholder_text(ta1, "username");
  lv_obj_add_event_cb(ta1,kb_appear,LV_EVENT_CLICKED,NULL);
  //密码文本框
  ta2 = lv_textarea_create(page2);
  lv_textarea_set_password_mode(ta2, true);
  lv_obj_set_pos(ta2,450,250);
  lv_obj_set_size(ta2,200,50);
  lv_textarea_set_password_show_time(ta2, 3000);
  lv_textarea_set_placeholder_text(ta2, "password");
  lv_obj_add_event_cb(ta2,kb_appear,LV_EVENT_CLICKED,NULL);
  //用户名与密码标签
  lv_obj_t *lb1=lv_label_create(page2);
  lv_obj_t *lb2=lv_label_create(page2);
  lv_obj_set_pos(lb1,370,160);
  show_chinese(20,lb1);
  lv_label_set_text(lb1, "用户名：");
  lv_obj_set_pos(lb2,380,260);
  show_chinese(20,lb2);
  lv_label_set_text(lb2, "密码：");
  //注册按钮
  lv_obj_t *bt1=lv_btn_create(page2);
  lv_obj_set_pos(bt1,180,400);
  lv_obj_set_size(bt1,150,50);
  lv_obj_set_style_bg_color(bt1, lv_color_hex(0x64c7c3), LV_STATE_DEFAULT);
  //注册按钮上标签
  lv_obj_t *rlb=lv_label_create(bt1);
  show_chinese(20,rlb);
  lv_label_set_text(rlb, "注册");
  //登录按钮
  lv_obj_t *bt2=lv_btn_create(page2);
  lv_obj_set_pos(bt2,430,400);
  lv_obj_set_size(bt2,150,50);
  lv_obj_set_style_bg_color(bt2, lv_color_hex(0x64c7c3), LV_STATE_DEFAULT);
  lv_obj_add_event_cb(bt2,usrname,LV_EVENT_CLICKED,NULL);
  //登录按钮上标签
  lv_obj_t *llb=lv_label_create(bt2);
  show_chinese(20,llb);
  lv_label_set_text(llb, "登录");
  //复选框
  lv_obj_t* checkbox1 = lv_checkbox_create(page2);
  lv_obj_set_pos(checkbox1,250,350);
  show_chinese(20,checkbox1);
  lv_checkbox_set_text(checkbox1, "同意《曼波与哈基米协议》");
  //软键盘
  kb = lv_keyboard_create(page2);
  lv_obj_add_event_cb(kb,kb_cb, LV_EVENT_CLICKED , NULL);
  lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
}

void quit_login(lv_event_t* e)
{
	//lv_obj_del(page1);
	show_login();
}

void show_search()
{
  mark2=1;
  //创建一个新页面
  page1=lv_obj_create(NULL);
  lv_obj_set_size(page1,800,480);
  lv_scr_load(page1);
  //吉祥物图片
  img3=lv_img_create(page1);
  lv_img_set_src(img3, "S:/myphoto/manbo2.bmp");
  lv_obj_set_pos(img3,500,100);
  //标题
  lv_obj_t* allb=lv_label_create(page1);
  lv_obj_set_pos(allb,150,50);
  show_chinese(20,allb);
  lv_label_set_text(allb, "欢迎使用曼波文件搜索系统");
  //音乐播放按钮矩阵
  lv_obj_t* btnmat=lv_btnmatrix_create(page1);
  lv_obj_set_pos(btnmat,500,400);
  lv_btnmatrix_set_map(btnmat,map1);
  lv_obj_add_event_cb(btnmat,btnmat_cb,LV_EVENT_CLICKED,NULL);
  //列表
  list = lv_list_create(page1);
  lv_obj_set_pos(list,80,100);
  lv_obj_set_size(list,400,450);
  show_chinese(20,list);
  if(mark==0)
	dirread("/mymusic",NULL);
  //搜索栏文本框
  ta4 = lv_textarea_create(page1);
  lv_obj_set_pos(ta4,500,270);
  lv_obj_set_size(ta4,250,50);
  lv_textarea_set_placeholder_text(ta4, "Something you want to search");
  lv_obj_add_event_cb(ta4,kb_appear,LV_EVENT_CLICKED,NULL);
  //搜索按钮
  bt=lv_btn_create(page1);
  //lv_obj_add_style(bt, &style_btn, LV_PART_MAIN);
  lv_obj_set_style_bg_color(bt, lv_color_hex(0x64c7c3), LV_STATE_DEFAULT);
  lv_obj_set_pos(bt,550,330);
  lv_obj_set_size(bt,100,50);
  lv_obj_add_event_cb(bt,search,LV_EVENT_CLICKED,NULL);
  //搜索按钮上标签
  lb=lv_label_create(bt);
  lv_label_set_text(lb,"search");
  //退出登录按钮
  bt5=lv_btn_create(page1);
  lv_obj_set_style_bg_color(bt5, lv_color_hex(0x64c7c3), LV_STATE_DEFAULT);
  lv_obj_set_pos(bt5,550,550);
  lv_obj_set_size(bt5,100,50);
  lv_obj_add_event_cb(bt5,quit_login,LV_EVENT_CLICKED,NULL);
  lb4=lv_label_create(bt5);
  lv_label_set_text(lb4,"quit");
  //软键盘
  kb2 = lv_keyboard_create(page1);
  lv_obj_add_event_cb(kb2,kb_cb, LV_EVENT_CLICKED , NULL);
  lv_obj_add_flag(kb2, LV_OBJ_FLAG_HIDDEN);
  lv_obj_align(kb2, LV_ALIGN_BOTTOM_MID, 0, 0);
}

int main(void)
{
    /*LittlevGL init*/
    lv_init();

    /*Linux frame buffer device init*/
    fbdev_init();

    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];

    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);

    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf   = &disp_buf;
    disp_drv.flush_cb   = fbdev_flush;
    disp_drv.hor_res    = 800;
    disp_drv.ver_res    = 480;
    lv_disp_drv_register(&disp_drv);

    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;

    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb = evdev_read;
    lv_indev_t *mouse_indev = lv_indev_drv_register(&indev_drv_1);


    // /*Set a cursor for the mouse*/
    // LV_IMG_DECLARE(mouse_cursor_icon)
    // lv_obj_t * cursor_obj = lv_img_create(lv_scr_act()); /*Create an image object for the cursor */
    // lv_img_set_src(cursor_obj, &mouse_cursor_icon);           /*Set the image source*/
    // lv_indev_set_cursor(mouse_indev, cursor_obj);             /*Connect the image  object to the driver*/


    // /*Create a Demo*/
    // lv_demo_widgets();
	
	show_login();

    /*Handle LitlevGL tasks (tickless mode)*/
    while(1) {
        lv_timer_handler();
        usleep(5000);
    }

    return 0;
}

/*Set in lv_conf.h as `LV_TICK_CUSTOM_SYS_TIME_EXPR`*/
uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if(start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
    return time_ms;
}
